package com.zhuangfei.hpuhelper.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;
import android.R.string;
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import cn.lzf.serviceplatform.R;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hpu.bean.Course;
import com.hpu.constants.ShareConstants;
import com.hpu.parser.CourseParser;
import com.hpu.tools.JsonTools;
import com.hpu.tools.ShareTools;
import com.zhuangfei.toolkit.tools.ToastTools;

/**
 * 课表工具
 * @author Administrator
 *
 */
public class CourseTools {
	
	/**
	 * 将大写的数字转化为整型数值
	 * 
	 * @param string
	 * @return
	 */
	public static int stringToInt(String string) {
		String s = "一二三四五六七八九十";
		int n = -1;
		if ((n = s.indexOf(string)) != -1)
			n = n + 1;
		else if (string.equals("十一"))
			n = 11;
		else if (string.equals("十二"))
			n = 12;
		return n;
	}

	public static void showAlert(Context context,Course course,List<Course> list,String courseString,String number){
		Toast.makeText(context, "course:"+course.getName()+"\n" +
				"list:"+(list==null?"null":list.size()+"")+"\n" +
				"number:"+number, Toast.LENGTH_SHORT).show();
	}
	@SuppressLint("NewApi")
	public static boolean saveCourse(Context context,Course course){
		if(course==null) return false;
		Gson gson=new Gson();
		List<Course> courseList=new ArrayList<>();
		
		String courses=getCourse(context, course.getTerm());
		if(!TextUtils.isEmpty(courses)) {
			courseList=gson.fromJson(courses,new TypeToken<List<Course>>(){}.getType());
		}

		if(courseList==null) return false;

		for(int i=0;i<courseList.size();i++){
			Course c=courseList.get(i);
			if(c.getDay()==course.getDay()&&c.getName()==course.getName()&&c.getRoom()==course.getRoom()&&c.getStart()==course.getStart()&&c.getStep()==course.getStep()&&c.getTeacher()==course.getTeacher()){
				return false;
			}
		}
		
		courseList.add(course);
		String number=(String) ShareTools.get(context,ShareConstants.STORE_USER, "默认用户");
		FileTools.writeCourse(number, course.getTerm(),gson.toJson(courseList) );
		return true;
	}
	
	public static void saveCourse(Context context,List<Course> courseList,String term){
		if(courseList==null) return;
		Gson gson=new Gson();
		String number=(String) ShareTools.get(context,ShareConstants.STORE_USER, "默认用户");
		
		FileTools.writeCourse(number, term,gson.toJson(courseList) );
	}
	
	public static void appendCourse(Context context,List<Course> courseList,String term){
		if(courseList==null) return;;
		Gson gson=new Gson();
		List<Course> list=new ArrayList<>();
		
		String courses=getCourse(context, term);
		
		if(!courses.equals("")) {
			list=gson.fromJson(courses,new TypeToken<List<Course>>(){}.getType());
		}
		
		String number=(String) ShareTools.get(context,ShareConstants.STORE_USER, "默认用户");
		list.addAll(courseList);
		FileTools.writeCourse(number, term,gson.toJson(list) );
	}
	
	public static String getCourse(Context context,String term){
		String number=(String) ShareTools.get(context,ShareConstants.STORE_USER, "默认用户");
		return FileTools.readCourse(number, term);
	}
	
	public static String getCourseJsonString(List<Course> list){
		return new Gson().toJson(list);
	}
	public static List<Course> getCourseFrom(String json){
		Gson gson=new Gson();
		List<Course> list=null;
		try {
			if(!TextUtils.isEmpty(json)) {
				list=gson.fromJson(json,new TypeToken<List<Course>>(){}.getType());
			}
		}catch (Exception e){
			if(list==null) return new ArrayList<>();
			else return list;
		}
		if(list==null) return new ArrayList<>();
		else return list;
	}
	
	public static List<Course> getCourseList(Context context,String term){
		String addCourse=CourseTools.getCourse(context,term);
		List<Course> myCourseList=null;
		try{
			if(!TextUtils.isEmpty(addCourse)){
				myCourseList=new Gson().fromJson(addCourse,new TypeToken<List<Course>>(){}.getType());
			}
		}catch (Exception e){
			if(myCourseList==null) return new ArrayList<>();
			else return myCourseList;
		}
		if(myCourseList==null) return new ArrayList<>();
		else return myCourseList;
	}

	public static String deleteCourse(Context context,List<Course> list,Course delete){
		if(list.indexOf(delete)==-1){
			return null;
		}

		list.remove(delete);
		Gson gson=new Gson();
		return gson.toJson(list);
	}


	public static List<Course> getNoTimeList(Context context,List<Course> list){
		List<Course> courses=new ArrayList<>();
		for(int i=0;i<list.size();i++){
			Course course=list.get(i);
			if(course.getDay()<=0||course.getStart()<=0||course.getStep()<=0){
				courses.add(course);
			}
		}
		return courses;
	}

	public static List<Course> getHaveTimeList(Context context,List<Course> list){
		List<Course> courses=new ArrayList<>();
		if(list==null) return courses;
		for(int i=0;i<list.size();i++){
			Course course=list.get(i);
			if(course.getWeekList()!=null&&course.getWeekList().size()!=0&&course.getDay()>0&&course.getStart()>0&&course.getStep()>0){
				courses.add(course);
			}
		}
		return courses;
	}
}
